﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SIGCBI.Entities;
using System.Data;
using System.Data.Entity;
using System.Data.Objects;

namespace SIGCBI.Repository
{
    public interface ICentroCostoRepository
    {
        List<CENTRO_COSTO> GetAllFromArea();
        List<CENTRO_COSTO> GetFromAreaByDescripcion(string descripcion);
        CENTRO_COSTO GetFromAreaById(Int32 id);
        void AddArea(CENTRO_COSTO area);
        void UpdateArea(CENTRO_COSTO area);
        void RemoveArea(Int32 id);
        bool ExisteDuplicidadAreaDescripcion(string areaDesc, Int32 id);
    }

    public class CentroCostoRepository : MasterDataAccess, ICentroCostoRepository
    {
        public List<CENTRO_COSTO> GetAllFromArea()
        {
            var query = from c in Context.CENTRO_COSTOs.Include("TRABAJADOR")
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }

        public List<CENTRO_COSTO> GetFromAreaByDescripcion(string descripcion)
        {
            if (descripcion != "")
            {
                var query = from c in Context.CENTRO_COSTOs.Include("TRABAJADOR")
                            where c.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query1 = from c in Context.CENTRO_COSTOs.Include("TRABAJADOR")
                             select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query1);

                return query1.ToList();
            }
        }

        public CENTRO_COSTO GetFromAreaById(int id)
        {
            var query = from p in Context.CENTRO_COSTOs
                        where p.ID.Equals(id)
                        select p;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }

        public void AddArea(CENTRO_COSTO area)
        {
            Context.CENTRO_COSTOs.Add(area);
            Context.SaveChanges();
        }

        public void UpdateArea(CENTRO_COSTO area)
        {
            Context.CENTRO_COSTOs.Attach(area);
            Context.Entry(area).State = EntityState.Modified;
            Context.SaveChanges();
        }

        public void RemoveArea(int id)
        {
            var existe = Context.CENTRO_COSTOs.Find(id);

            Context.CENTRO_COSTOs.Remove(existe);
            Context.SaveChanges();
        }

        public bool ExisteDuplicidadAreaDescripcion(string areaDesc, int id)
        {
            var query = from c in Context.CENTRO_COSTOs
                        where c.Descripcion.ToUpper().CompareTo(areaDesc.ToUpper()) == 0 && c.ID != id
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.Any();
        }
    }
}
